package com.heima.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeign;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *  TODO
 * @author wangjian
 * @since 2022-06-28
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {
    @Override
    public ResponseResult listStatus(AuthDto dto) {
        //构建分页插件
        IPage<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        // 根据状态进行查询 进行判断状态是否为空
        if (dto.getStatus() != null) {
            queryWrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        IPage<ApUserRealname> iPage = this.page(page, queryWrapper);
        // 构建通用的分页响应
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(),
                iPage.getTotal(), iPage.getRecords());
        return result;
    }

    @Autowired
    private IApUserService apUserService;

    @Autowired
    private MediaFeign mediaFeign;

    @Autowired
    private ArticleFeign articleFeign;

    @Override
    @GlobalTransactional
    public ResponseResult auth(AuthDto dto, int type) {
        //type = 0 表示驳回    = 1 表示通过
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser apUserById = apUserService.getById(dto.getId());
        if (apUserById == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据ApUser 的ID查询ApUserRealName
        LambdaQueryWrapper<ApUserRealname> realnameLambdaQueryWrapper = new LambdaQueryWrapper<>();
        realnameLambdaQueryWrapper.eq(ApUserRealname::getUserId, dto.getId());
        ApUserRealname apUserRealname = this.getOne(realnameLambdaQueryWrapper);
        if (type == 0) {
            apUserRealname.setStatus(2);
            apUserRealname.setReason(dto.getMsg());
            this.updateById(apUserRealname);
        } else {
        }
        //            1、远程调用自媒体微服务，创建自媒体人
        WmUser wmUser = createWmUser(apUserById);
//            2、远程调用文章微服务，创建文章作者
        ApAuthor apAuthor = createApAuthor(apUserById, wmUser);
        wmUser.setApAuthorId(apAuthor.getId());
        mediaFeign.updateWmUser(wmUser);
//            4、修改ap_user_realname用户的认证状态
        apUserRealname.setStatus(9);
        this.updateById(apUserRealname);
//            5、修改ap_user用户的认证状态
        apUserById.setFlag(1); // "0 普通用户            1 自媒体人            2 大V"
        apUserById.setIsIdentityAuthentication(true);
        apUserService.updateById(apUserById);
      return ResponseResult.okResult();
    }
    private ApAuthor createApAuthor(ApUser apUser, WmUser wmUser) {
        //ApAuthor apAuthor = new ApAuthor();
        ApAuthor apAuthor = new ApAuthor();
        apAuthor.setName(apUser.getName());
        apAuthor.setType(2);
        apAuthor.setUserId(apUser.getId());
        apAuthor.setWmUserId(wmUser.getId());
        ResponseResult<ApAuthor> responseResult = articleFeign.saveApAuthor(apAuthor);
        if (responseResult.getCode().equals(0)) {
            return responseResult.getData();
        }else{
            throw new RuntimeException("远程调用失败");
        }
    }

    private WmUser createWmUser(ApUser apUser) {
        WmUser wmUser = new WmUser();
        wmUser.setApUserId(apUser.getId());
        wmUser.setName(apUser.getName());
        wmUser.setPassword(apUser.getPassword());
        wmUser.setSalt(apUser.getSalt());
        wmUser.setImage(apUser.getImage());
        wmUser.setPhone(apUser.getPhone());
        wmUser.setStatus(9); // 0 暂时不可用            1 永久不可用            9 正常可用
        wmUser.setType(0); //"账号类型            0 个人             1 企业            2 子账号"
        wmUser.setScore(0);
        ResponseResult<WmUser> responseResult = mediaFeign.saveWmUser(wmUser);
        if (responseResult.getCode().equals(0)) {
            return responseResult.getData();
        }else{
            throw new RuntimeException("远程调用失败");
        }
    }

}


